{% extends "base.html" %}

{% block css %}
    <style>
        /* 按钮悬浮*/
        .float-button {
            position: fixed;
            bottom: 100px;
            right: 160px;
            z-index: 99;
            border: none;
            outline: none;
            background-color: #1dc5a3;
            cursor: pointer;
            padding: 7px;
            border-radius: 10px;
        }

        .reset {
            right: 100px;
        }

        .btn-primary:hover {
            color: red;
        }
    </style>
{% endblock %}

{% block nav %}
    {% for object in model_list %}
        <li><a href="{{ object.url }}"><i class="{{ object.icon }}"></i>{{ object.model_name }}</a>
        </li>

    {% endfor %}

{% endblock %}

{% block crumbs %}
    <div class="row">
        <div class="col-md-12">
            <ol class="breadcrumb">
                <li class="active">
                    <a class="this-page" href="{% url 'base:interface' %}">接口管理</a>
                </li>
                <li class="active">
                    <a class="this">编辑接口</a>
                </li>
            </ol>
        </div>
    </div>
{% endblock %}

{% block body %}

    <div class="row">
        <div class="col-md-12">
            <div class="panel">
                <div class="panel-heading">
                    <h3 class="panel-title">编辑接口</h3>
                </div>
                <div class="panel-body">
                    <input name="if_id" id="if_id" value={{ interface.if_id }} hidden>
                    <div class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">接口名称</label>
                            <div class="col-sm-8">
                                <input type="text" id="if_name" name="if_name" class="form-control"
                                       value="{{ interface.if_name }}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">所属项目</label>
                            <div class="col-sm-8">
                                <select type="text" id="prj_id" name="prj_id" class="form-control">
                                    <option value="{{ interface.project_id }}">{{ interface.project }}</option>
                                    {% for prj in prj_list %}
                                        <option value={{ prj.prj_id }}>{{ prj.prj_name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">接口路径</label>
                            <div class="col-sm-8">
                                <input type="text" id="url" name="url" class="form-control"
                                       value="{{ interface.url }}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">请求方式</label>
                            <div class="col-sm-1 control-label">
                                <label class="fancy-radio">
                                    {% if method == 0 %}
                                        <input name="method" value="get" type="radio" checked="true">
                                        <span><i></i>get</span>
                                    {% else %}
                                        <input name="method" value="get" type="radio"><span><i></i>get</span>
                                    {% endif %}
                                </label>
                            </div>
                            <div class="col-sm-1 control-label">
                                <label class="fancy-radio">
                                    {% if method == 1 %}
                                        <input name="method" value="post" type="radio" checked="true">
                                        <span><i></i>post</span>
                                    {% else %}
                                        <input name="method" value="post" type="radio"><span><i></i>post</span>
                                    {% endif %}
                                </label>
                            </div>
                            <div class="col-sm-1 control-label">
                                <label class="fancy-radio">
                                    {% if method == 2 %}
                                        <input name="method" value="delete" type="radio" checked="true">
                                        <span><i></i>delete</span>
                                    {% else %}
                                        <input name="method" value="delete" type="radio"><span><i></i>delete</span>
                                    {% endif %}
                                </label>
                            </div>
                            <div class="col-sm-1 control-label">
                                <label class="fancy-radio">
                                    {% if method == 3 %}
                                        <input name="method" value="put" type="radio" checked="true">
                                        <span><i></i>put</span>
                                    {% else %}
                                        <input name="method" value="put" type="radio"><span><i></i>put</span>
                                    {% endif %}
                                </label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">数据传输方式</label>
                            <div class="col-sm-8">
                                <select type="text" id="data_type" name="data_type" class="form-control">
                                    <option value="{{ interface.data_type }}">{{ interface.data_type }}</option>
                                    <option value='json'>json</option>
                                    <option value='data'>data</option>
                                    <option value='sql'>sql</option>
                                    <option value='file'>file</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" title="为空执行，有内容不执行！">跳过</label>
                            <div class="col-sm-8">
                                <input type="text" id="skip" name="skip" class="form-control" value="{{ interface.skip }}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">是否签名</label>
                            <div class="col-sm-2 control-label">
                                <label class="fancy-radio">
                                    {% if is_sign == 1 %}
                                        <input name="is_sign" value="1" type="radio" checked><span><i></i>签名</span>
                                    {% else %}
                                        <input name="is_sign" value="1" type="radio"><span><i></i>签名</span>
                                    {% endif %}
                                </label>
                            </div>
                            <div class="col-sm-2 control-label">
                                <label class="fancy-radio">
                                    {% if is_sign == 0 %}
                                        <input name="is_sign" value="0" type="radio" checked><span><i></i>不签名</span>
                                    {% else %}
                                        <input name="is_sign" value="0" type="radio"><span><i></i>不签名</span>
                                    {% endif %}
                                </label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">设置headers</label>
                            <div class="col-sm-2 control-label">
                                <label class="fancy-radio">
                                    {% if is_headers == 1 %}
                                        <input name="is_headers" value="1" type="radio" checked>
                                        <span><i></i>设置</span>
                                    {% else %}
                                        <input name="is_headers" value="1" type="radio"><span><i></i>设置</span>
                                    {% endif %}
                                </label>
                            </div>
                            <div class="col-sm-2 control-label">
                                <label class="fancy-radio">
                                    {% if is_headers == 0 %}
                                        <input name="is_headers" value="0" type="radio" checked>
                                        <span><i></i>不设置</span>
                                    {% else %}
                                        <input name="is_headers" value="0" type="radio"><span><i></i>不设置</span>
                                    {% endif %}
                                </label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">是否mock</label>
                            <div class="col-sm-2 control-label">
                                <label class="fancy-radio">
                                    {% if mock == "1" %}
                                        <input name="mock" value="1" type="radio" checked>
                                        <span><i></i>是的</span>
                                    {% else %}
                                        <input name="mock" value="1" type="radio"><span><i></i>是的</span>
                                    {% endif %}
                                </label>
                            </div>
                            <div class="col-sm-2 control-label">
                                <label class="fancy-radio">
                                    {% if mock == "0" %}
                                        <input name="mock" value="0" type="radio" checked>
                                        <span><i></i>不是的</span>
                                    {% else %}
                                        <input name="mock" value="0" type="radio"><span><i></i>不是的</span>
                                    {% endif %}
                                </label>
                            </div>
                        </div>
                        {% if interface.response_header_param %}
                            <div class="form-group">
                                <label class="col-sm-2 control-label">swagger接口tags</label>
                                <div class="col-sm-8">
                                    <input type="text" id="tags" name="tags" class="form-control"
                                           value="{{ interface.response_header_param }}" disabled="disabled">
                                </div>
                            </div>
                        {% endif %}
                        <div class="form-group">
                            <label class="col-sm-2 control-label">接口描述</label>
                            <div class="col-sm-8">
                                <textarea name="description" class="form-control"
                                          rows="3">{{ interface.description }}</textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">请求header</label>
                            <div class="col-sm-8">
                                <button id="add_request_header" class="btn btn-primary" type="button"><i
                                        class="fa fa-plus-square-o"></i>&nbsp;添加行
                                </button>
                                <button id="add_request_header_json" class="btn btn-primary" type="button"><i
                                        class="fa fa-plus-square-o"></i>&nbsp;添加json
                                </button>
                            </div>
                            <div class="col-sm-offset-2 col-sm-8">
                                <table id="request_header" class="table col-sm-10" id="request_header_table">
                                    <thead>
                                    <tr>
                                        <th>头部标签</th>
                                        {#                                        <th>备注</th>#}
                                        <th></th>
                                    </tr>

                                    {% if request_header_param_list %}
                                        {% for request_header_param in request_header_param_list %}
                                            <tr>
                                                <td name='var_name'
                                                    contenteditable='true'>{{ request_header_param.var_name }}</td>
                                                {#                                                <td name=''#}
                                                {#                                                    contenteditable='true'>{{ request_header_param.var_remark }}</td>#}
                                                <td>
                                                    <button class='btn btn-danger' onclick='del_step(this)'><i
                                                            class='fa fa-close'></i>&nbsp;删除行
                                                    </button>
                                                </td>
                                            </tr>
                                        {% endfor %}
                                    {% endif %}

                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>

                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">请求body</label>
                            <div class="col-sm-8">
                                <button id="add_request_body" class="btn btn-primary" type="button"><i
                                        class="fa fa-plus-square-o"></i>&nbsp;添加行
                                </button>
                                <button id="add_request_body_json" class="btn btn-primary" type="button"><i
                                        class="fa fa-plus-square-o"></i>&nbsp;添加json
                                </button>
                            </div>
                            <div class="col-sm-offset-2 col-sm-8">
                                <table id="request_body" class="table col-sm-10">
                                    <thead>
                                    <tr>
                                        <th>参数名</th>
                                        <th></th>
                                    </tr>
                                    {% if request_body_param_list %}
                                        {% for request_body_param in request_body_param_list %}
                                            <tr>
                                                <td name='var_name'
                                                    contenteditable='true'>{{ request_body_param.var_name }}</td>
                                                <td>
                                                    <button class='btn btn-danger' onclick='del_step(this)'><i
                                                            class='fa fa-close'></i>&nbsp;删除行
                                                    </button>
                                                </td>
                                            </tr>
                                        {% endfor %}
                                    {% endif %}
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>

                        </div>
                        {#                        <div class="form-group">#}
                        {#                            <label class="col-sm-2 control-label">返回header</label>#}
                        {#                            <div class="col-sm-8">#}
                        {#                                <button id="add_response_header" class="btn btn-outline btn-primary" type="button">#}
                        {#                                    <i class="fa fa-plus-square-o"></i>&nbsp;添加行#}
                        {#                                </button>#}
                        {#                                <button id="add_response_header_json" class="btn btn-primary" type="button"><i#}
                        {#                                        class="fa fa-plus-square-o"></i>&nbsp;添加json#}
                        {#                                </button>#}
                        {#                            </div>#}
                        {#                            <div class="col-sm-offset-2 col-sm-8">#}
                        {#                                <table id="response_header" class="table col-sm-10">#}
                        {#                                    <thead>#}
                        {#                                    <tr>#}
                        {#                                        <th>头部标签</th>#}
                        {#                                        <th></th>#}
                        {#                                    </tr>#}
                        {#                                    {% if response_header_param_list %}#}
                        {#                                        {% for response_header_param in response_header_param_list %}#}
                        {#                                            <tr>#}
                        {#                                                <td name='var_name'>{{ response_header_param }}</td>#}
                        {#                                                <td>#}
                        {#                                                    <button class='btn btn-danger' onclick='del_step(this)'><i#}
                        {#                                                            class='fa fa-close'></i>&nbsp;删除行#}
                        {#                                                    </button>#}
                        {#                                                </td>#}
                        {#                                            </tr>#}
                        {#                                        {% endfor %}#}
                        {#                                    {% endif %}#}
                        {#                                    </thead>#}
                        {#                                    <tbody>#}
                        {#                                    </tbody>#}
                        {#                                </table>#}
                        {#                            </div>#}
                        {##}
                        {#                        </div>#}
                        {#                        <div class="form-group">#}
                        {#                            <label class="col-sm-2 control-label">返回body</label>#}
                        {#                            <div class="col-sm-8">#}
                        {#                                <button id="add_response_body" class="btn btn-outline btn-primary" type="button"><i#}
                        {#                                        class="fa fa-plus-square-o"></i>&nbsp;添加行#}
                        {#                                </button>#}
                        {#                                <button id="add_response_body_json" class="btn btn-primary" type="button"><i#}
                        {#                                        class="fa fa-plus-square-o"></i>&nbsp;添加json#}
                        {#                                </button>#}
                        {#                            </div>#}
                        {#                            <div class="col-sm-offset-2 col-sm-8">#}
                        {#                                <table id="response_body" class="table col-sm-10">#}
                        {#                                    <thead>#}
                        {#                                    <tr>#}
                        {#                                        <th>参数名</th>#}
                        {#                                        <th></th>#}
                        {#                                    </tr>#}
                        {#                                    {% if response_body_param_list %}#}
                        {#                                        {% for response_body_param in response_body_param_list %}#}
                        {#                                            <tr>#}
                        {#                                                <td name='var_name'#}
                        {#                                                    contenteditable='true'>{{ response_body_param.var_name }}</td>#}
                        {#                                                <td>#}
                        {#                                                    <button class='btn btn-danger' onclick='del_step(this)'><i#}
                        {#                                                            class='fa fa-close'></i>&nbsp;删除行#}
                        {#                                                    </button>#}
                        {#                                                </td>#}
                        {#                                            </tr>#}
                        {#                                        {% endfor %}#}
                        {#                                    {% endif %}#}
                        {#                                    </thead>#}
                        {#                                    <tbody>#}
                        {#                                    </tbody>#}
                        {#                                </table>#}
                        {#                            </div>#}
                        {##}
                        {#                        </div>#}
                        <div class="form-group">
                            <div class="col-sm-offset-4 col-sm-6">
                                <button id="submit" class="btn btn-primary float-button" ><i
                                        class="fa fa-save"></i>&nbsp;保存
                                </button>
                                <button id="reset" type="reset" class="btn btn-primary float-button reset"><i
                                        class="fa fa-repeat"></i>&nbsp;重置
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}
{% block javascript %}
    <script type="text/javascript">
        //重置 刷新页面
        $('#reset').click(function () {
            location.reload();
        });

        function del_step(obj) {
            var tr = obj.parentNode.parentNode;
            var tbody = tr.parentNode;
            tbody.removeChild(tr);
        }

        function analysis_request_header(obj) {
            var request_header_json = $("textarea[name='add_request_header_json']").val();
            $.ajax({
                url: "/base/findata/",
                type: "post",
                data: {
                    "request_header_json": request_header_json,
                    "type": "analysis_request_header_json"
                },
                success: function (data) {
                    if (data == 'no') {
                        alert('请求头解析参数错误，请检查解析的文本是否是json格式！')
                    }
                    else {
                        $("#request_header tr:last").empty();
                        for (var params in data) {
                            html = "<tr> <td name='var_name' contenteditable='true'>" + data[params] + "</td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";
                            $("#request_header tr:last").after(html);
                        }
                    }
                },
                error: function () {
                    alert("error!");
                }
            });
        }

        function analysis_request_body(obj) {
            var request_header_json = $("textarea[name='add_request_body_json']").val();
            $.ajax({
                url: "/base/findata/",
                type: "post",
                data: {
                    "request_header_json": request_header_json,
                    "type": "analysis_request_header_json"
                },
                success: function (data) {
                    if (data == 'no') {
                        alert('请求body解析参数错误，请检查解析的文本是否是json格式！')
                    }
                    else {
                        $("#request_body tr:last").empty();
                        for (var params in data) {
                            html = "<tr> <td name='var_name' contenteditable='true'>" + data[params] + "</td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";
                            $("#request_body tr:last").after(html);
                        }
                    }
                },
                error: function () {
                    alert("error!");
                }
            });
        }

        {#        function analysis_response_header(obj) {#}
        {#            var request_header_json = $("textarea[name='add_response_header_json']").val();#}
        {#            $.ajax({#}
        {#                url: "/base/findata/",#}
        {#                type: "post",#}
        {#                data: {#}
        {#                    "request_header_json": request_header_json,#}
        {#                    "type": "analysis_request_header_json"#}
        {#                },#}
        {#                success: function (data) {#}
        {#                    if (data == 'no') {#}
        {#                        alert('返回头解析参数错误，请检查解析的文本是否是json格式！')#}
        {#                    }#}
        {#                    else {#}
        {#                        $("#response_header tr:last").empty();#}
        {#                        for (var params in data) {#}
        {#                            html = "<tr> <td name='var_name' contenteditable='true'>" + data[params] + "</td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
        {#                            $("#response_header tr:last").after(html);#}
        {#                        }#}
        {#                    }#}
        {#                },#}
        {#                error: function () {#}
        {#                    alert("error!");#}
        {#                }#}
        {#            });#}
        {#        }#}
        {##}
        {#        function analysis_response_body(obj) {#}
        {#            var request_header_json = $("textarea[name='add_response_body_json']").val();#}
        {#            $.ajax({#}
        {#                url: "/base/findata/",#}
        {#                type: "post",#}
        {#                data: {#}
        {#                    "request_header_json": request_header_json,#}
        {#                    "type": "analysis_request_header_json"#}
        {#                },#}
        {#                success: function (data) {#}
        {#                    if (data == 'no') {#}
        {#                        alert('返回body解析参数错误，请检查解析的文本是否是json格式！')#}
        {#                    }#}
        {#                    else {#}
        {#                        $("#response_body tr:last").empty();#}
        {#                        for (var params in data) {#}
        {#                            html = "<tr> <td name='var_name' contenteditable='true'>" + data[params] + "</td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
        {#                            $("#response_body tr:last").after(html);#}
        {#                        }#}
        {#                    }#}
        {#                },#}
        {#                error: function () {#}
        {#                    alert("error!");#}
        {#                }#}
        {#            });#}
        {#        }#}

        $(document).ready(function () {
            //添加请求头参数行      <td name='var_remark' contenteditable='true'></td>

            $("#add_request_header").click(function () {
                var newRow = "<tr> <td name='var_name' contenteditable='true'></td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";
                $("#request_header tr:last").after(newRow);
            });
            $("#add_request_header_json").click(function () {
                {#                var newRow = "<tr> <td name='var_name' contenteditable='true'></td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
                var request_header_json = $("textarea[name='add_request_header_json']").val();
                if (typeof(request_header_json) == 'undefined') {
                    var newRow = "<tr><td><textarea  name='add_request_header_json' placeholder='请输入请求头的json参数' class='form-control' rows='3'></textarea></td><td><button class='btn btn-primary' onclick='analysis_request_header(this)'><i class='fa fa-gavel'></i>&nbsp;解析</button> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td></tr>";
                    $("#request_header tr:last").after(newRow);
                }
            });

            $("#add_request_body").click(function () {
                var newRow = "<tr> <td name='var_name' contenteditable='true'></td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";
                $("#request_body tr:last").after(newRow);
            });
            $("#add_request_body_json").click(function () {
                {#                var newRow = "<tr> <td name='var_name' contenteditable='true'></td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
                var request_body_json = $("textarea[name='add_request_body_json']").val();
                if (typeof(request_body_json) == 'undefined') {
                    var newRow = "<tr><td><textarea  name='add_request_body_json' placeholder='请输入请求body的json参数' class='form-control' rows='3'></textarea></td><td><button class='btn btn-primary' onclick='analysis_request_body(this)'><i class='fa fa-gavel'></i>&nbsp;解析</button> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td></tr>";
                    $("#request_body tr:last").after(newRow);
                }
            });

            {#            $("#add_response_header").click(function () {#}
            {#                var newRow = "<tr> <td name='var_name' contenteditable='true'></td><td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
            {#                $("#response_header tr:last").after(newRow);#}
            {#            });#}
            {#            $("#add_response_header_json").click(function () {#}
            {#                var newRow = "<tr> <td name='var_name' contenteditable='true'></td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
            {#                var response_header_json = $("textarea[name='add_response_header_json']").val();#}
            {#                if (typeof(response_header_json) == 'undefined') {#}
            {#                    var newRow = "<tr><td><textarea  name='add_response_header_json' placeholder='请输入返回头的json参数' class='form-control' rows='3'></textarea></td><td><button class='btn btn-primary' onclick='analysis_response_header(this)'><i class='fa fa-gavel'></i>&nbsp;解析</button> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td></tr>";#}
            {#                    $("#response_header tr:last").after(newRow);#}
            {#                }#}
            {#            });#}
            {##}
            {#            $("#add_response_body").click(function () {#}
            {#                var newRow = "<tr> <td name='var_name' contenteditable='true'></td><td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
            {#                $("#response_body tr:last").after(newRow);#}
            {#            });#}
            {#            $("#add_response_body_json").click(function () {#}
            {#                var newRow = "<tr> <td name='var_name' contenteditable='true'></td> <td> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td> </tr>";#}
            {#                var response_body_json = $("textarea[name='add_response_body_json']").val();#}
            {#                if (typeof(response_body_json) == 'undefined') {#}
            {#                    var newRow = "<tr><td><textarea  name='add_response_body_json' placeholder='请输入返回body的json参数' class='form-control' rows='3'></textarea></td><td><button class='btn btn-primary' onclick='analysis_response_body(this)'><i class='fa fa-gavel'></i>&nbsp;解析</button> <button class='btn btn-danger' onclick='del_step(this)'><i class='fa fa-close'></i>&nbsp;删除行</button> </td></tr>";#}
            {#                    $("#response_body tr:last").after(newRow);#}
            {#                }#}
            {#            });#}
            //删除行
        });

        $('#get_table_data').click(function () {


            var args = {};
            $('#request_header tr').each(function (i) {                   // 遍历 tr
                if (i == 0) return true;
                var data = new Object();
                $(this).find("td[name]").each(function () {
                    var name = $(this).attr("name");
                    data[name] = $(this).text();
                });
                args[i] = data;
            });
            send_data = JSON.stringify(args);
            alert(send_data);
        });

        $('#submit').click(function () {

            var if_name = $("#if_name").val();
            var if_id = $("#if_id").val();
            var prj_id = $("#prj_id").val();
            var url = $("#url").val();
            var method = $("input[name='method']:checked").val();
            var data_type = $("#data_type").val();
            var skip = $("#skip").val();
            var is_sign = $("input[name='is_sign']:checked").val();
            var is_headers = $("input[name='is_headers']:checked").val();
            var mock = $("input[name='mock']:checked").val();
            var description = $("textarea[name='description']").val();

            var request_header_data;
            var request_body_data;
            {#            var response_header_data;#}
            {#            var response_body_data;#}

            var request_header_json = $("textarea[name='add_request_header_json']").val();
            if (typeof(request_header_json) == 'undefined') {

                var args = [];
                $('#request_header tr').each(function (i) {
                    if (i == 0) return true;
                    var data = new Object();
                    $(this).find("td[name]").each(function () {
                        var name = $(this).attr("name");
                        data[name] = $(this).text();
                    });

                    if (JSON.stringify(data) != '{}') {
                        args.push(data);
                    }
                });
                request_header_data = JSON.stringify(args);
            } else {
                alert('请求头参数未解析或者为空，不可以保存！');
                return
            }

            var request_body_json = $("textarea[name='add_request_body_json']").val();
            if (typeof(request_body_json) == 'undefined') {
                var args = [];
                $('#request_body tr').each(function (i) {
                    if (i == 0) return true;
                    var data = new Object();
                    $(this).find("td[name]").each(function () {
                        var name = $(this).attr("name");
                        data[name] = $(this).text();
                    });
                    if (JSON.stringify(data) != '{}') {
                        args.push(data);
                    }
                });
                request_body_data = JSON.stringify(args);
            } else {
                alert('请求body参数未解析或者为空，不可以保存！');
                return
            }

            {#            var response_header_json = $("textarea[name='add_response_header_json']").val();#}
            {#            if (typeof(response_header_json) == 'undefined') {#}
            {#                var args = [];#}
            {#                $('#response_header tr').each(function (i) {                   // 遍历 tr#}
            {#                    if (i == 0) return true;#}
            {#                    var data = new Object();#}
            {#                    $(this).find("td[name]").each(function () {#}
            {#                        var name = $(this).attr("name");#}
            {#                        data[name] = $(this).text();#}
            {#                    });#}
            {#                    if (JSON.stringify(data) != '{}') {#}
            {#                        args.push(data);#}
            {#                    }#}
            {#                });#}
            {#                response_header_data = JSON.stringify(args);#}
            {#            } else {#}
            {#                alert('返回头参数未解析或者为空，不可以保存！');#}
            {#                return#}
            {#            }#}
            {##}
            {#            var response_body_json = $("textarea[name='add_response_body_json']").val();#}
            {#            if (typeof(response_body_json) == 'undefined') {#}
            {#                var args = [];#}
            {#                $('#response_body tr').each(function (i) {                   // 遍历 tr#}
            {#                    if (i == 0) return true;#}
            {#                    var data = new Object();#}
            {#                    $(this).find("td[name]").each(function () {#}
            {#                        var name = $(this).attr("name");#}
            {#                        data[name] = $(this).text();#}
            {#                    });#}
            {#                    if (JSON.stringify(data) != '{}') {#}
            {#                        args.push(data);#}
            {#                    }#}
            {#                });#}
            {#                response_body_data = JSON.stringify(args);#}
            {#            } else {#}
            {#                alert('返回body参数未解析或者为空，不可以保存！');#}
            {#                return#}
            {#            }#}

            $.ajax({
                url: "/base/interface_update/",
                type: "post",
                data: {
                    "if_name": if_name,
                    "if_id": if_id,
                    "prj_id": prj_id,
                    "url": url,
                    "method": method,
                    "data_type": data_type,
                    "is_sign": is_sign,
                    "is_headers": is_headers,
                    "mock": mock,
                    "skip": skip,
                    "description": description,
                    "request_header_data": request_header_data,
                    "request_body_data": request_body_data,
                    {#                    "response_header_data": response_header_data,#}
                    {#                    "response_body_data": response_body_data#}
                },
                success: function (data) {
                    if (data != "ok") {
                        alert(data);
                        window.location.reload()
                    } else {
                        alert("保存成功");
                        window.location.replace("/base/interface/?page={{ page }}");
                    }
                },
                error: function () {
                    alert("error!");
                }
            });

        });

    </script>
{% endblock %}
